// JavaScript Document
/* exported addfile */

var size=0;
$(function () {

    //上传按钮
    $('#submit').bind("click", function () {
        if ($(".file").length == 0) {
            $.alert("请添加文件");
            return;
        }
        if ($("#fileName").val()=="" || $("#fileName").val()==undefined) {
            $.alert("请添加文件名");
            return;
        }

        var formData = new FormData();
        let  files =$(".file");
        let  number=1;

         let suffix="";
        for (let i=0;i<files.length;i++) {
            number++;
            var file = $(files[i]).children(".inp_file")[0].files[0];
            size+=file.size;
            let index= file.name.lastIndexOf(".");
            let ext = file.name.substr(index+1);
            if(suffix==""){
                suffix=ext;
            }else {
                if(ext!=suffix){
                    $.alert("文件后缀名不一致");
                    return;
                }
            }
            formData.append("file", file);
        }

        formData.append("fileName", $("#fileName").val());

            $.ajax({
                url: "/upload",
                type: 'POST',
                data: formData,
                // 告诉jQuery不要去处理发送的数据
                processData: false,
                // 告诉jQuery不要去设置Content-Type请求头
                contentType: false,
                processData: false,
                contentType: false,
                xhr: function () {
                    var xhr = $.ajaxSettings.xhr();

                    if (xhr.upload) {
                        //处理进度条的事件
                       xhr.upload.addEventListener("progress", progressHandle, false);
                        //加载完成的事件
                        xhr.addEventListener("load", completeHandle, false);
                        //加载出错的事件
                        xhr.addEventListener("error", failedHandle, false);
                        //加载取消的事件
                        xhr.addEventListener("abort", canceledHandle, false);
                        //开始显示进度条
                        showProgress();
                        return xhr;
                    }
                },
                beforeSend: function () {
                    console.log("正在进行，请稍候");
                }, success: function (responseStr) {
                    console.log(responseStr)
                    if(responseStr=="false"){
                        $.alert("文件上传失败");
                    }else{
                        let path=responseStr.substring(0,responseStr.lastIndexOf("/"));
                        let name=responseStr.substring(path.length+1,responseStr.lastIndexOf("_"))
                        let name2=responseStr.substring(responseStr.lastIndexOf("."),responseStr.length)
                        let fileName=name+name2;
                        let protoType=parseInt($("#playRate").val());
                        let referenceTime=Math.ceil(size/2/(protoType*1000*1000));
                        let Sample={
                            "id":null,
                            "referenceFreq":$("#referenceFreq").val(),
                            "referencePower":$("#referencePower").val(),
                            "modulationType":$("#modulationType").val(),
                            "codType":$("#modulationType").val(),
                            "protoType":$("#protoType").val(),
                            "sourceType":$("#sourceType").val(),
                            "tranModel":$("#tranModel").val(),
                            "carrierNum":$("#carrierNum").val(),
                            "systemType":$("#systemType").val(),
                            "referenceTime":referenceTime,
                            "fileName":$("#fileName").val(),
                            "fileNumber":files.length,
                            "filePath":path,
                            "fileSize":size,
                            "playRate":$("#playRate").val(),
                            "name":fileName,
                            "getData": endData.Format('yyyy-MM-dd hh:mm:ss'),
                        }
                        $.ajax({
                           url:"Sample/save",
                            contentType: "application/json;charset=UTF-8",
                            type: "post",
                            async: false,
                            data: JSON.stringify(Sample),
                            dataType: "json",
                            success:function (data) {
                                console.log(data);
                                $(".popWindow").hide();
                                $(":input").val("");
                                $(".ui.filelist").empty();
                                if(data>0){
                                    alert("添加成功");
                                    history.go(0)
                                }else {
                                    alert("添加失败");
                                    history.go(0)
                                }

                            },

                        })


                    }

                }
            });

    });
});



//初始化绑定事件按钮


function showProgress() {
    $(".progress-body").css("display", "block");
}
//隐藏进度条的函数
function hideProgress() {
    $('.progress-body>.progress-speed', window.parent.document).html("0 bytes");
    $('.progress-body>.progress-info', window.parent.document).html("资料正准备上传");
    $(".progress-body", window.parent.document).css("display", "none");
}
//进度条更新
function progressHandle(e) {
    $('.progress-body progress').attr({value: e.loaded, max: e.total});
    var percent = e.loaded / e.total * 100;
    $('.progress-body>.progress-speed').html(((e.loaded / 1024) / 1024).toFixed(2) + "/" + ((e.total / 1024) / 1024).toFixed(2) + " MB. " + percent.toFixed(2) + "%");
    if (percent == 100) {
        $('.progress-body>.progress-info').html("上传完成,后台正在处理...");
    } else {
        $('.progress-body>.progress-info').html("资料上传ing...");
    }
};
//上传完成处理函数
function completeHandle(e) {
    $('.progress-body>.progress-info').html("上传资料完成");
    setTimeout(hideProgress, 2000);

};
//上传出错处理函数
function failedHandle(e) {
    $('.progress-body>.progress-info', window.parent.document).html("上传资源出错");
};
//上传取消处理函数
function canceledHandle(e) {
    $('.progress-body>.progress-info', window.parent.document).html("上传资源取消");
};





